package com.own.util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FieldUtil {
	public static Class getGenericClass(Field field) {
		Class refCls = null;
		try {
			Type type = field.getGenericType();
			ParameterizedType pt = (ParameterizedType) type;
			refCls = (Class) pt.getActualTypeArguments()[0];
		} catch (Exception e) {
		}
		return refCls;
	}

	public static Field[] getAllFields(Class klass) {
		List<Field> fields = new ArrayList<Field>();
		fields.addAll(Arrays.asList(klass.getDeclaredFields()));
		if (klass.getSuperclass() != null) {
			fields.addAll(Arrays.asList(getAllFields(klass.getSuperclass())));
		}
		Collections.reverse(fields);
		return fields.toArray(new Field[] {});
	}

}
